package com.briup.web.servlet;

import com.briup.bean.Customer;
import com.briup.service.ICustomerService;
import com.briup.service.impl.CustomerServiceImpl;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet("/login")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ICustomerService customerService = new CustomerServiceImpl();
        String password = req.getParameter("password");
        String name = req.getParameter("name");
        HttpSession session = req.getSession();
        try {
            Customer customer = customerService.login(name, password);
            session.setAttribute("customer",customer);
            //如果成功的话 跳转到index
            resp.sendRedirect("index.jsp");
        }catch (Exception e){
            e.printStackTrace();
            String message = e.getMessage();
            session.setAttribute("error",message);
//            如果有一场 跳转到login.jsp
            resp.sendRedirect("login.jsp");
        }
    }
}
